<template>
    <div>
        <a-form :form="form" layout="inline" @submit.prevent="onSubmit" :style="{marginBottom: '20px'}" id="role-search">
            <a-row>
                <a-col :span="16">
                    <a-row>
                        <a-col :span="8">
                            <a-form-item>
                                <a-input placeholder="请输入角色编号或名称" v-decorator="['name']"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="6">
                            <a-form-item>
                                <a-select v-decorator="['type']" placeholder="角色类型">
                                    <a-select-option v-for="item in types" :value="item.id">
                                        {{item.name}}
                                    </a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :span="6">
                            <a-form-item>
                                <a-select v-decorator="['status']" placeholder="角色状态">
                                    <a-select-option value="1">
                                        可用
                                    </a-select-option>
                                    <a-select-option value="0">
                                        停用
                                    </a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :span="4">
                            <a-form-item>
                                <a-button type="primary" html-type="submit">筛选</a-button>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-col>
                <a-col :span="8">
                    <a-form-item :style="{float: 'right'}">
                        <a-button type="primary" v-if="menu_role.includes('/role/create')" icon="plus" @click="showCreate">新增角色</a-button>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>

        <a-table :columns="columns" :dataSource="tableData" rowKey="id" :pagination="pagination" bordered>
            <span slot="action" slot-scope="text, record">
                <div class="delete-image">
                <router-link :to="'/role-update/'+record.id" v-if="menu_role.includes('/role-update')"><img src="./../../assets/images/view.svg"
                                                                  alt=""></router-link>
                <a href="#" @click.stop.prevent="showModal(record)" v-if="record.id !=='1' && menu_role.includes('/role/delete')"><img
                        src="./../../assets/images/delete.svg" alt=""></a>
                </div>
            </span>
        </a-table>

        <a-modal v-model="showcreate" title="新增角色" @ok="createSubmit" @cancel="cancel"
                 centered
                 :cancelButtonProps="{ props: {type: 'primary'}}"
                 :keyboard = 'false'
                 okText = '确认'
                 :maskClosable = 'false'>
            <create :types="types" @getList="getList" @hideCreate="hideCreate" ref="create"></create>
        </a-modal>
        <a-modal
            v-model='show_delete'
            title='删除角色'
            @ok = 'handleDelete'
            width="450px"
            :cancelButtonProps="{ props: {type: 'primary'}}"
            centered
            :keyboard = 'false'
            okText = '删除'
            :maskClosable = 'false'
        >
            <div class="confirm-delete-wrapper">
                <p>是否删除此角色？</p>
            </div>
        </a-modal>
    </div>
</template>


<script>
    import Create from './create'
    export default {
        components: {Create},
        data() {
            return {
                is_admin:this.$store.getters.is_admin,
                pagination: {
                    current: 1,
                    total: 0,
                    pageSize: 10,
                    showQuickJumper: true,
                    onChange: (page, size) => {
                        this.handleChange(page, size)
                    },
                },
                form: this.$form.createForm(this),
                tableData: [],
                columns: [
                    {title: '角色编号', dataIndex: 'sn', align: 'center', width: '20%'},
                    {title: '角色名称', dataIndex: 'name', align: 'center', width: '15%'},
                    {title: '角色类型', dataIndex: 'type', align: 'center', width: '15%'},
                    {title: '状态', dataIndex: 'status', align: 'center', width: '20%'},
                    {title: '添加时间', dataIndex: 'created_at', align: 'center', width: '20%'},
                    {title: '操作', scopedSlots: {customRender: 'action'}, align: 'center', width: '10%'},
                ],
                search: {},
                deleteRow: {},
                types: [],
                showcreate: false,
                show_delete: false,
                
            };
        },

        mounted() {
            this.getList();
        },
        methods: {
            async getList(param = {}) {
                let req = {page_size: this.pagination.pageSize, page: this.pagination.current};

                let data = await this.http(
                    "get",
                    "role/index",
                    Object.assign(req, param)
                );
                this.tableData = data.data;
                this.types = data.type
                this.pagination.total = data.total_count;
            },
            showModal(row) {
                this.show_delete = true;
                this.deleteRow = row;
            },
            cancel() {
                this.$refs.create.form.resetFields();
            },
            onSubmit() {
                //搜索提交

                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.pagination.current = 1;
                        this.search = values
                        this.getList(values);
                    }
                })
            },
            showCreate() {
                this.showcreate = true
            },
            hideCreate() {
                this.showcreate = false
            },
            createSubmit() {
                //新增提交
                this.$refs.create.onSubmit()
                
            },
            handleChange(page, size) {
                //列表展示条数改变
                this.pagination.current = page;
                this.pagination.pageSize = size;
                this.getList(this.search);
            },
            handleRole(row) {
                this.$router.push("role-list/" + row.id);
            },
            handleDelete() {
                let that = this;
                let row = this.deleteRow;

                that.deleteData(row.id).then(res => {
                    if (res.status == "success") {
                        that.$message.success(res.msg, 1);
                        that.getList();
                    } else {
                        that.$message.error(res.msg, 1);
                    }
                    this.show_delete = false;
                });
            },
            async deleteData(id) {
                return await this.http("delete", "role/delete", {id: id});
            }
        }
    };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    #role-search {
        .ant-input {
            width: 242px;
        }

        .ant-select {
            width: 180px;
        }
    }

    .confirm-delete-wrapper {
        width: 100%;
        padding: 20px;
        text-align: center;
        p {
            font-size: 16px;
        }
    }

    @media screen and (max-width: 1366px) {
        #role-search {
            .ant-input {
                width: 200px;
            }

            .ant-select {
                width: 148px;
            }
        }
    }
</style>
